package com.bianguo.android.bprinter.view.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.bianguo.android.baselibrary.utils.MLog;
import com.bianguo.android.bprinter.utils.ScreenUtil;

import java.util.ArrayList;

public class ZoomViewLayout2 extends RelativeLayout implements ZoomView2.OnFocusListener, ZoomView2.OnClickPointListener {

    private ArrayList<ZoomView2> zoomViews;
    private int lastHeight;
    private OnClickCutPoint onClickCutPoint;

    public ZoomViewLayout2(Context context) {
        this(context, null);
    }

    public ZoomViewLayout2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZoomViewLayout2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        zoomViews = new ArrayList<>();
    }

    public void addZoomView(Bitmap bitmap, boolean fromRes) {
        lastHeight = getMaxHeight();
        ZoomView2 zoomView = new ZoomView2(getContext());
        zoomView.addBitmap(bitmap);
        zoomView.setOnFocusListener(this);
        zoomView.setOnClickPointListener(this);
        addView(zoomView);
        zoomViews.add(0, zoomView);

//        if (!fromRes) {
//            WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
//            DisplayMetrics dm = new DisplayMetrics();
//            wm.getDefaultDisplay().getMetrics(dm);
//            zoomView.postScale(160 / dm.densityDpi);
//        }
        int width = bitmap.getWidth();
        float scale = ScreenUtil.getScreenWidth(getContext()) * 1.0f / width;
        zoomView.postScale(scale);
        zoomView.postTranslate(lastHeight);
        sortZ();
    }

    public void updateBitmap(ZoomView2 zoomView, Bitmap bitmap) {
        zoomView.updateBitmap(bitmap);
    }

    @Override
    public void onFocus(View view) {
        zoomViews.remove(view);
        zoomViews.add(0, (ZoomView2) view);
        sortZ();
    }

    @Override
    public void onClickPoint(View view, int pointType) {
        switch (pointType) {
            case ZoomView.CLICK_POINT_TYPE_DELETE:
                zoomViews.remove(view);
                removeView(view);
                sortZ();
                break;
            case ZoomView.CLICK_POINT_TYPE_CUT:
                if (onClickCutPoint != null) {
                    onClickCutPoint.onClickCutPoint((ZoomView2) view);
                }
                break;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        setMeasuredDimension(ScreenUtil.getScreenWidth(getContext()), getMaxHeight());
    }

    private void sortZ() {
        int size = zoomViews.size();
        for (int i = 0; i < size; i++) {
            ZoomView2 zoomView = zoomViews.get(i);
            zoomView.setElevation(size - i);
        }
    }

    public int getMaxHeight() {
        int height = 0;
        if (zoomViews.isEmpty()) {
            int childCount = getChildCount();
            if (childCount <= 0) {
                return 0;
            }
            for (int i = 0; i < childCount; i++) {
                View childView = getChildAt(i);
                height += childView.getHeight();
            }
            return height;
        }
        for (ZoomView2 zoomView : zoomViews) {
            int bottomY = zoomView.getBottomY();
            if (height < bottomY) {
                height = bottomY;
            }
        }
        View childView = getChildAt(0);
        int childViewHeight = childView.getHeight();
        return height > childViewHeight ? height : childViewHeight;
    }

    public void setOnClickCutPoint(OnClickCutPoint onClickCutPoint) {
        this.onClickCutPoint = onClickCutPoint;
    }

    public interface OnClickCutPoint {
        void onClickCutPoint(ZoomView2 zoomView);
    }
}
